package com.expect.admin.busmanagement.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.expect.admin.busmanagement.data.dao.BusRepository;
import com.expect.admin.busmanagement.data.dataobject.Bus;
import com.expect.admin.busmanagement.service.vo.BusVo;
import com.expect.admin.data.dao.AttachmentRepository;
import com.expect.admin.data.dao.UserRepository;
import com.expect.admin.data.dataobject.Attachment;
import com.expect.admin.data.dataobject.User;
import com.expect.admin.exception.BaseAppException;
import com.expect.admin.service.LcService;
import com.expect.admin.service.LcrzbService;
import com.expect.admin.service.UserService;
import com.expect.admin.service.vo.LcrzbVo;
import com.expect.admin.service.vo.UserVo;
import com.expect.admin.utils.DateUtil;
import com.expect.admin.utils.StringUtil;

@Service
public class BusService {
//	@Autowired
//	private BusRepository busRepository;
	@Autowired
	private UserService userService;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private LcService lcService;
	@Autowired
	private LcrzbService lcrzbService;
	@Autowired
	private AttachmentRepository attachmentRepository;
	
	@Transactional
	public String save(BusVo busVo, String[] attachmentId){
		Bus bus = new Bus(busVo);
		UserVo userVo = userService.getLoginUser();
		User user = userRepository.findOne(userVo.getId());
		bus.setUserName(user);
		
		/**
		 * 处理维保申请和附件的对应关系
		 */
		if(attachmentId != null && attachmentId.length > 0){
			List<Attachment> attachmentList = attachmentRepository.findByIdIn(attachmentId);
			if(attachmentList != null && attachmentList.size() > 0){
				bus.setAttachments(new HashSet<>(attachmentList));
			}
		}else{
			bus.setAttachments(new HashSet<>());
		}
//		bus = busRepository.save(bus);
		return bus.getBusId();
	}
	
	/**
	 * 新维保申请的保存
	 * @param busVo
	 * @param saveWay   保存种类（提交：“submit”，保存：“save”）
	 * @param attachmentId
	 */
	@Transactional
	public void newApplicationSave(BusVo busVo, String saveWay, String[] attachmentId){
		String applicationClassify = getApplicationClassify();
		busVo.setApplicationClassify(applicationClassify);
		String lcbs = applicationClassify;
		String condition = getNewBusCondition(saveWay,lcbs);
		if(StringUtil.equals(saveWay, "submit")){
			busVo.setApplicationDate(DateUtil.format(new Date(), DateUtil.fullFormat));
		}
		String busId = save(busVo,attachmentId);
		busVo.setBusId(busId);
		if(StringUtil.equals(saveWay, "submit")){
			addXzLcrz(busId,busVo.getApplicationClassify(),lcService.getStartCondition(lcbs));
		}
	}

	/**
	 * 新增时增加流程日志
	 * @param busId
	 * @param applicationClassify
	 * @param applicationState
	 */
	@Transactional
	private void addXzLcrz(String busId,String applicationClassify,String applicationState) {
		LcrzbVo lcrzbVo = new LcrzbVo("新增","");
		String lcrzId = lcrzbService.save(lcrzbVo, busId, applicationClassify, applicationState);
		bindBusWithLcrz(busId,lcrzId);	
	}

	/**
	 * 流程日志与维保申请的绑定
	 * @param busId
	 * @param lcrzId
	 */
	private void bindBusWithLcrz(String busId, String lcrzId) {
		// TODO Auto-generated method stub
		
	}

	private String getNewBusCondition(String saveWay, String lcbs) {
		// TODO Auto-generated method stub
		return null;
	}

	private String getApplicationClassify() {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	public void updateApplication(BusVo busVo, String[] attachmentId) {
		if(busVo == null || StringUtil.isBlank(busVo.getBusId())){
			throw new BaseAppException("要更新的申请Id为空，无法更新");
		}
//		Bus bus = busRepository.findOne(busVo.getBusId());
//		if(bus == null){
//			throw new BaseAppException("要更新的申请不存在！");
//		}
//		update(bus,busVo);
		//更新申请的附件列表
		if(attachmentId != null && attachmentId.length > 0){
			List<Attachment> attachmentList = attachmentRepository.findByIdIn(attachmentId);
			if(attachmentList != null && attachmentList.size() > 0){
//				bus.setAttachments(new HashSet<>(attachmentList));
			}
		}
//		busRepository.save(bus);
	}

	private void update(Bus bus, BusVo busVo) {
		// TODO Auto-generated method stub
		
	}

	@Transactional
	public void deleteApplication(String id) {
		// TODO Auto-generated method stub
		
	}

	public BusVo getBusById(String busId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	public void delete(String id) {
		// TODO Auto-generated method stub
		
	}

	public Object getBusByUserIdAndCondition(String id, String jdId, String state) {
		// TODO Auto-generated method stub
		return null;
	}
}
